    const mysql1 = require('../db/index')
    const marked = require('marked')
        // 发表文章
    const articlePage = (req, res) => {
            if (!req.session.isLogin) return res.redirect('/')
            res.render('./article/add.ejs', {
                user: req.session.user,
                isLogin: req.session.isLogin
            })
        }
        // z添加新文章
    const addArticle = (req, res) => {
        var body = req.body
            // 如果发表文章时候编写的文章很长时间，则session可能会失效
            // body.authorld = req.session.user.id
        body.ctime = mysql1.moment().format('YYYY-MM-DD HH:mm:ss')
        var sql = 'insert into blog_atricles set ?'
        mysql1.conn.query(sql, body, function(err, result) {
            if (err) return res.send({ msg: '发表文章失败', status: 500 })
            if (result.affectedRows !== 1) return res.send({ msg: '发表文章失败', status: 501 })
            res.send({ msg: '发表文章成功', status: 200, insertId: result.insertId })
        })
    }
    const infoPage = (req, res) => {
            // 获取文章id
            const id = req.params.id
                // 根据id查询文章信息
            const sql = 'select * from blog_atricles where id=?'
            mysql1.conn.query(sql, id, (err, result) => {
                if (err) return res.send({ msg: '获取文章失败', status: 500 })
                if (result.length !== 1) return res.redirect('/')
                    // 在调用res.render方法之前，要先把Markdown文本转为html文本
                const html = marked(result[0].content)
                result[0].content = html
                res.render('./article/info.ejs', { user: req.session.user, isLogin: req.session.isLogin, article: result[0] })
            })
        }
        // 展示编辑页面
    const editPage = (req, res) => {
            if (!req.session.isLogin) return res.redirect('/')
            const sql = 'select * from blog_atricles where id=?'
            var a = req.params.id
            mysql1.conn.query(sql, a, (err, result) => {
                if (err) return res.redirect('/')
                if (result.length !== 1) return res.redirect('/')
                res.render('./article/edit.ejs', { user: req.session.user, isLogin: req.session.isLogin, article: result[0] })
            })
        }
        // 更新编辑页面
    const editPage1 = (req, res) => {
        if (!req.session.isLogin) return res.redirect('/')
        const sql = 'update blog_atricles set ?  where id=?'
        mysql1.conn.query(sql, [req.body, req.body.id], (err, result) => {
            console.log('err', err)
            if (err) return res.redirect('/')
            if (result.affectedRows !== 1) return res.redirect('/')
            res.send({ msg: '发表成功', status: 200 })
        })
    }
    module.exports = {
        articlePage,
        addArticle,
        infoPage,
        editPage,
        editPage1
    }